Spatial interaction represent the dynamic flow or movementmof people, material, or information between locations in geographical space. Each spatial interaction is composed of a discrete origin and destination (OD) pair and represented as a spatial interaction matrix of centroids from origin and destination. The connection between origin and their destination can be visualized by desire lines, typically straight lines that records the the number of people travelling between locations.
Spatial Interaction Models are mathematical models for estimating flows between spatial entities.
There are four main types of spatial interaction models: 1. Unconstrained 2. Production-constrained 3. Attraction-constrained 4. Doubly-constrained
Learning Objectives 1. Build a spatial interaction matrix 2. Construct desire lines in geospatial data 3. Compute a distance matrix
1.2 Load packages
sf performs geospatial data import, integration, processing and transformation. DT enables R data objects (matrices or data frames) to be displayed as tables on HTML pages. tidyverse performs data import, integration, wrangling and visualisation. tmap creates thematic maps. stplanranalyses OD matrix.
pacman::p_load(tmap, sf, DT, stplanr, tidyverse)
The downloaded binary packages are in
/var/folders/yb/8mcgsm5s7dn_6hxtm7czbmy40000gn/T//Rtmpzezvks/downloaded_packages
The downloaded binary packages are in
/var/folders/yb/8mcgsm5s7dn_6hxtm7czbmy40000gn/T//Rtmpzezvks/downloaded_packages
odbus is an aspatial dataset containing the number of trips by weekdays and weekends from origin to destination bus stops. It reflects the passenger trip traffic and the most recent dataset from September 2023 will be used.
The output indicates 5,714,196 records and 7 fields. The bus stop codes are converted into factor for data handling.
busstops is a geospatial dataset that contains the detailed information for all bus stops currently serviced by buses, including bus stop code, road name, description, location coordinates. The output indicates that the geospatial objects are point features. There are 5161 features and 3 fields. It is in SVY21 projected coordinates system with XY dimension.
mpsz is a geospatial dataset from the Master Plan 2019, a forward looking guiding plan for Singapore’s development in the medium term over the next 10 to 15 years published in 2019. Note this mpsz differs from that in previous chapter, Data Wrangling.
The output indicates that the geospatial objects are multipolygon features. There are 332 features and 6 fields. It is in WGS84 projected coordinates system with XY dimension.
Commuting flows on weekday between 6am to 9am are extracted and there are 226,658 records during weekday afternoon peak hours based on bus stop code. The data table can be viewed using datatable() and saved to rds using write_rds().
RDS files have two main advantages over CSV files: 1. RDS takes up less disk space. 2. RDS format handles different types of objects, such as fitted models or statistical test results, while CSVs can only save rectangular data, such as data frames.
write_rds exports output in rds format.
write_rds(odbus69, "rds/odbus69.rds")
read_rds imports rds files.
odbus69 <-read_rds("rds/odbus69.rds")
To integrate the planning subzone codes SUBZONE_C of mpsz sf data frame into busstop sf data frame, st_intersection() is used to perform point and polygon overlay and the output will be in point sf object. select() retain only BUS_STOP_N and SUBZONE_C in the busstop_mpsz sf data frame. There are 5,156 bus stops in the subzones. st_drop_geometryremove the geometric information from the output and return the aspatial data.
Based on origin bus stop codes, append the planning subzone code from busstop_mpsz data frame onto od_data data frame using left_join(). There are 239,219 commuting flows records consisting of 5,020 origin bus stops.
Duplicates are removed by retaining unique() records. From previous 239,219 records, 577 rows containing duplication are removed and 238,642 unique records remain in od_data.
Based on destination bus stop codes, append the planning subzone code from busstop_mpsz data frame onto od_data data frame using left_join(). Summing the trips by origin and destination, there are 21,112 OD flows during weekday morning peak hour.
od2line() takes a data frame and a spatial object as inputs and outputs geographic lines, also known as desire lines, to represent movement between origins and destinations.
flowline <-od2line(flow = od_data1, zones = mpsz,zone_code ="SUBZONE_C")